package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.FoundGoodContentBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.HomeMarketPresenter;
import cn.xfdzx.android.apps.shop.presenter.MarketBottomPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.StringUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：批发底部商品
 */
public class    MarketGoodsAdapter extends RecyclerView.Adapter<MarketGoodsAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<FoundGoodContentBeen.DataBean> mList;
    private Activity mContext;
    private int proportion;
    private MarketBottomPresenter marketPresenter;
    private RoundedFormation transformation;
    private RoundedFormation transformation1;
    private MultiTransformation<Bitmap> mation;
    private RequestOptions options;


    public MarketGoodsAdapter(Activity mContext, List<FoundGoodContentBeen.DataBean> list, MarketBottomPresenter marketPresenter) {
        this.mList = list;
        this.mContext = mContext;
        this.marketPresenter = marketPresenter;
        //顶部左边圆角
        transformation = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_LEFT);
        //顶部右边圆角
        transformation1 = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_RIGHT);
        //组合各种Transformation,
        mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(), transformation, transformation1);
        options = RequestOptions.bitmapTransform(mation).override(100 * proportion, 100);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    public void setData() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_market_goods, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        holder.ttvContent.setText(mList.get(position).getTitle());
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFoundGood);

        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            holder.tvPrice.setText("¥" + mList.get(position).getMarketprice()+"/"+mList.get(position).getUnit());
            holder.tvOldPrice.setVisibility(View.GONE);
            holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getMarketprice(),mList.get(position).getGoods_weight()) + "/斤");
        } else {
            holder.tvPrice.setText("¥" + mList.get(position).getDiscountprice());
            holder.tvOldPrice.setVisibility(View.VISIBLE);
            holder.tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.tvOldPrice.setText("¥" + mList.get(position).getMarketprice()+"/"+mList.get(position).getUnit());
            holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getDiscountprice(),mList.get(position).getGoods_weight())+ "/斤");
        }
        holder.tvSaleNum.setText("已售" + mList.get(position).getSale_num() + "件");
        holder.tv_maozhong.setText("毛重: " + mList.get(position).getGoods_weight() + "斤");

        RoundedCorners roundedCorners = new RoundedCorners(20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getStore_logo()).apply(options.transform(new CircleCrop(), roundedCorners)).into(holder.ivShop);

        holder.tvShopName.setText(mList.get(position).getStore_name());
        holder.tvShopBase.setVisibility(View.GONE);

        holder.tvCarNum.setText(mList.get(position).getAdd_cart_num() + "");
        if (mList.get(position).getIs_self_store()==1) {//自营
            holder.tv_ziying.setVisibility(View.VISIBLE);
            holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());
        } else {
            holder.tv_ziying.setVisibility(View.GONE);
            if (null == mList.get(position).getGoods_ensures()) {
                holder.ttvContent.setText(mList.get(position).getTitle());
            } else {
                holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());
            }
        }

        if (mList.get(position).getAdd_cart_num() == 0) {
            holder.tvCarNum.setVisibility(View.GONE);
        } else {
            holder.tvCarNum.setVisibility(View.VISIBLE);
        }
        if (mList.get(position).getIs_close() == 1 || mList.get(position).getStock() == 0) {
            //已售罄
            holder.ivCar.setImageResource(R.drawable.icon_home_pifa_che_hui);
            holder.ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.viewBgHui.setVisibility(View.VISIBLE);
            holder.tvBgHui.setVisibility(View.VISIBLE);
        } else {
            holder.viewBgHui.setVisibility(View.GONE);
            holder.tvBgHui.setVisibility(View.GONE);
            holder.ivCar.setImageResource(R.drawable.icon_home_pifa_che_lv);
            holder.ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (EmptyUtil.isEmpty(APP_TOKEN)) {
                        Intent intentlogin = new Intent(mContext, OneLoginMainActivity.class);
                        mContext.startActivity(intentlogin);
                        return;
                    }
                    ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(mContext);
                    shoppingCartAnimationView.setPiFaColor();
                    int postion[] = new int[2];
                    holder.ivCar.getLocationInWindow(postion);
                    shoppingCartAnimationView.setStartPosition(new Point(postion[0], postion[1]));
                    ViewGroup rootView = (ViewGroup) mContext.getWindow().getDecorView();
                    rootView.addView(shoppingCartAnimationView);
                    shoppingCartAnimationView.setEndPosition(new Point(UtilsData.getPreference(mContext, "main_car").getInteger("tv_car_position_left"), UtilsData.getPreference(mContext, "main_car").getInteger("tv_car_position_right")));
                    shoppingCartAnimationView.startBeizerAnimation();
                    if (mList.get(position).getIs_self_store()==1) {//自营
                        if (mList.get(position).getAdd_cart_num() >= mList.get(position).getIs_support()) {
                            marketPresenter.addCar("0", "", mList.get(position).getId() + "", "1", position,mList.get(position).getAdd_cart_num()+1);
                        } else {
                            marketPresenter.addCar("0", "", mList.get(position).getId() + "",mList.get(position).getIs_support()+"", position, mList.get(position).getAdd_cart_num()+mList.get(position).getIs_support());
                        }
                    } else {
                        marketPresenter.addCar("0", "", mList.get(position).getId() + "", "1", position,mList.get(position).getAdd_cart_num()+1);
                    }
                }
            });
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {

        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_content)
        TagTextView ttvContent;
        @BindView(R.id.iv_found_good)
        ImageView ivFoundGood;
        @BindView(R.id.iv_shop)
        ImageView ivShop;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_old_price)
        TextView tvOldPrice;
        @BindView(R.id.tv_sale_num)
        TextView tvSaleNum;
        @BindView(R.id.tv_car_num)
        TextView tvCarNum;
        @BindView(R.id.iv_car)
        ImageView ivCar;
        @BindView(R.id.tv_ziying)
        TextView tv_ziying;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_shop_base)
        TextView tvShopBase;
        @BindView(R.id.view_bg_hui)
        View viewBgHui;
        @BindView(R.id.tv_bg_hui)
        TextView tvBgHui;
        @BindView(R.id.tv_aboutweight)
        TextView tv_aboutweight;
        @BindView(R.id.tv_maozhong)
        TextView tv_maozhong;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
